package com.zl.learning.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.text.ParseException;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by zhaolei on 2017/7/4.
 */
public class MySchedulerTest {
    private static SchedulerFactory schedulerFactory = new StdSchedulerFactory();
    private static Scheduler scheduler;

    static {
        try {
            scheduler = schedulerFactory.getScheduler();
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    public JobKey addJob(Class<? extends Job> cla, String time) {
        JobDetail jobDetail = JobBuilder.newJob(cla)
                .withIdentity(cla.getName(), cla.getName())
                .build();
        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity(cla.getName(), cla.getName())
                .withSchedule(CronScheduleBuilder.cronSchedule(time)).build();
        try {
            scheduler.scheduleJob(jobDetail, cronTrigger);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        return jobDetail.getKey();
    }

    public static void main(String[] args) throws SchedulerException, InterruptedException {
        MySchedulerTest test = new MySchedulerTest();
        JobKey key1 = test.addJob(MyJob1.class, "*/2 * * * * ?");
//        JobKey key2 = test.addJob(MyJob2.class, "*/1 * * * * ?");
//        Scheduler scheduler = schedulerFactory.getScheduler();
//            scheduler.pauseAll();
//            scheduler.shutdown();
//        scheduler.pauseJob(key1);
        Thread.sleep(2000);
        scheduler.deleteJob(key1);
        scheduler.deleteJob(key1);
        System.out.println("delete key1");
//        Thread.sleep(2000);
        scheduler.shutdown();
//        key1 = test.addJob(MyJob1.class, "*/2 * * * * ?");
    }

    static class Test1 {
        public static void main(String[] args) {
            try {
                MySchedulerTest test = new MySchedulerTest();
                Scheduler scheduler = schedulerFactory.getScheduler();
                JobKey key1 = test.addJob(MyJob1.class, "*/2 * * * * ?");
                TimeUnit.MINUTES.sleep(1);
                scheduler.shutdown();
            } catch (SchedulerException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class Test2 {
        public static void main(String[] args) throws ParseException {
            CronExpression cronExpression = new CronExpression("0 0/5 * * * ?");
            List<Date> dateList = new ArrayList<>();
            Date date = new Date();
            for (int i = 0; i < 100; i++) {
                Date date1 = cronExpression.getNextValidTimeAfter(date);
                dateList.add(date1);
                date = date1;
            }
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            dateList.forEach(r -> System.out.println(r.toInstant().atZone(ZoneId.systemDefault()).format(formatter)));
        }
    }
}

